<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <scroll class="content" ref="sc">
      <!--    轮播图-->
      <home-swiper :banners="banners"/>
      <!--    推荐信息-->
      <recommend-view :recommends="recommends"/>
      <!--    本周流行-->
      <feature-view/>
      <!--   TabControl(公共业务组件）-->
      <tab-control class="tab-control" :titles="['流行', '精选', '新款']" @tabClick="tabClick"/>
      <!--   商品列表页-->
      <goods-list :goods="showGoodsList"/>
    </scroll>
    <!-- @click.native 监听组件的原生事件 -->
    <back-top @click.native="backClick" />
  </div>
</template>

<script>
  // 这里注意根据不同的引入注意分类方便维护
  import HomeSwiper from "@/views/home/childComponent/HomeSwiper";
  import RecommendView from "@/views/home/childComponent/RecommendView";
  import FeatureView from "@/views/home/childComponent/FeatureView";

  import NavBar from "@/components/common/navBar/NavBar";
  import TabControl from "@/components/content/tabControl/TabControl";
  import GoodsList from "@/components/content/goods/GoodsList";
  import Scroll from "@/components/common/scroll/Scroll";
  import BackTop from "@/components/content/backTop/BackTop";

  import { getHomeMultiData, getHomeGoods } from "@/network/home";


  export default {
    name: "Home",
    components: {
      HomeSwiper,
      RecommendView,
      FeatureView,
      NavBar,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    computed: {
      showGoodsList() {
        return this.goods[this.currentType].list
      }
    },
    data() {
      return {
        // 轮播图数据
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []}
        },
        currentType: 'pop'
      }
    },
    created() {
      // 1. 请求多个数据
      this.getHomeMultiData()
      // 2. 请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')
    },
    methods: {
      /*
        事件监听相关
      */
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break;
          case 1:
            this.currentType = 'new'
            break;
          case 2:
            this.currentType = 'sell'
            break;
        }
      },
      /*
        网络请求相关
      */
      getHomeMultiData() {
        // 这里是一个异步函数，且这个函数的返回值是promise数据
        getHomeMultiData().then(res => {
          // console.log(res);
          this.banners = res.data.data.banner.list
          this.recommends = res.data.data.recommend.list
        })
      },
      getHomeGoods(type) {
        const page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.data.list)
          this.goods[type].page += 1
          // console.log(this.goods[type].list);
        })
      },
      backClick() {
        this.$refs.sc.scrollTop(0, 0, 500)
      }
    }
  }
</script>

<style scoped>
  #home {
    height: 100vh;
    position: relative;
    padding-top: 44px;
  }
  .home-nav {
    background-color: var(--color-tint);
    color: white;
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    z-index: 9;
  }
  .tab-control {
    position: sticky;
    top: 44px;
  }
  /* 这里使用定位来适配所有的手机 */
  .content {
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
</style>
